<?php
class Warehouse_Model_Area
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Warehouse_Model_DbTable_Area();
	}
	
	
	/**
	*Create A Record  
	*@param rowSet $row warehouse_Area's data 
	*@return boolean
	*/
	public  function create($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row warehouse_Area's data 
	*@param int $wa_id 
	*@return boolean
	*/
	public  function update($row,$id){
	
		$where = $this->_table->getAdapter()->quoteInto("wa_id= ?", $id);
 
		return $this->_table->update($row, $where);
		
	}
		
	/**
	*Delete One Row
	*@param int $wa_id 
	*@return boolean
	*/
	public function delete($id){
	
		$where = $this->_table->getAdapter()->quoteInto("wa_id= ?", $id);	 
 
		return $this->_table->delete($where);
		
	}

    public function getByCondition($condition,$type='*',$orderby="warehouse_area.wa_id",$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
		$select->joinLeft('product_option_choose', 'warehouse_area.poc_id = product_option_choose.poc_id', array('product_option_choose.poc_value'));
        $select->where("1 =?",1);
		if(isset($condition["wa_name"]) && $condition["wa_name"]!=''){
            $select->where(" warehouse_area.wa_name =?",$condition["wa_name"]);
        }
		if(isset($condition["wa_code"]) && $condition["wa_code"]!=''){
            $select->where(" warehouse_area.wa_code =?",$condition["wa_code"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();

		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}

    public function getByConditionAll($condition,$type='*',$pageSize=0,$page=1,$orderby="wa_id"){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        if(isset($condition["wa_name"]) && $condition["wa_name"]!=''){
            $select->where(" wa_name =?",$condition["wa_name"]);
        }
        //in array()
        if(isset($condition["poc_id"]) && $condition["poc_id"]!=''){
            $select->where(" poc_id in(?)",$condition["poc_id"]);
        }
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"]!=''){
            $select->where(" warehouse_id =?",$condition["warehouse_id"]);
        }
        if(isset($condition["wa_code"]) && $condition["wa_code"]!=''){
            $select->where(" wa_code =?",$condition["wa_code"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
            $sql=$select->__toString();

            return $this->_table->getAdapter()->fetchAll($sql);
        }

    }
	
	/**
	*Get Rowset By warehouse_Area 
	*@param string $ura_id 
	*@return Rowset
	*/
	public function getById($id){
		$where = $this->_table->getAdapter()->quoteInto('wa_id= ?', $id);
		return $this->_table->fetchRow($where);
	}
	
	public function getByWaCode($code){
		$where = $this->_table->getAdapter()->quoteInto('wa_code= ?', $code);
		return $this->_table->fetchRow($where);
	}
	
	public function getByfield($field=1,$value=1){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchAll($where);
	}
	
	public function getByWarehouseId($id){
		$where = $this->_table->getAdapter()->quoteInto('warehouse_id= ?', $id);
		return $this->_table->fetchAll($where)->toArray();
	}	
		
}